﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Core.Extensions;
using App.Domain;
using App.Web.ViewModels;
using App.Web.Utility;
using AutoMapper;

namespace App.Web.Controllers
{
    public class NewsLetterController : BaseController
    {
		private AppEntities _Context = new AppEntities();
        
		#region Constructors
		public NewsLetterController()
		{
		}

        public NewsLetterController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods

        [HttpPost]
        public int SaveNewsLetter(string email)
        {
            NewsLetter newsLetter = new NewsLetter();
            newsLetter = _Context.NewsLetters.FirstOrDefault(P => P.Email == email);

            if (newsLetter == null)
            {
                NewsLetter nl = new NewsLetter();

                nl.Email = email;
                nl.CreatedOn = DateTime.Now;
                nl.IsActive = true;

                _Context.AddToNewsLetters(nl);
                _Context.SaveChanges();

                return 1;
            }

            else
            {
                newsLetter.IsActive = true;

                _Context.SaveChanges();

                return 1;
            }
        }

		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
